﻿using ComLib.ConfigService;
using LogLib;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;

namespace PassportReader
{
    public class PassportReaderOption : IniConfigBase
    {
        public override string ConfigDirectory
        {
            get
            {
                string directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);
                directory = directory.Substring(6);
                return directory;
            }
        }

        public override string ConfigFilePath
        {
            get
            {
                if (string.IsNullOrEmpty(_strConfigPath))
                {
                    string className = this.GetType().Name;
                    _strConfigPath = Path.Combine(ConfigDirectory, className + ".ini");
                }
                return _strConfigPath;
            }
        }

        private string _userId = "";
        public string UserId
        {
            get { return _userId; }
            set { _userId = value; }
        }

        private string _dllDir = "";
        public string DllDir
        {
            get { return _dllDir; }
            set { _dllDir = value; }
        }

        private int _scanInterval = 200;
        public int ScanInterval
        {
            get { return _scanInterval; }
            set { _scanInterval = value; }
        }

        public override bool Save()
        {
            base.Save();
            SaveField("UserId", _userId);
            SaveField("DllDir", _dllDir);
            SaveField("ScanInterval", _scanInterval);
            return true;
        }

        public override bool Load()
        {
            base.Load();
            try
            {
                _userId = LoadStringValue("UserId");
                _dllDir = LoadStringValue("DllDir");
                _scanInterval = LoadInt32Value("ScanInterval");
            }
            catch (Exception ex)
            {
                string strMsg = string.Format("配置文件{0}加载错误:\n\t{1}", ConfigFilePath, ex.Message);
                Log.WriteException(ex.ToString());
                return false;
            }
            return true;
        }

        public override string ToString()
        {
            StringBuilder builder = new StringBuilder("护照阅读器选项: ");
            builder.Append("[UserId]:" + _userId);
            builder.Append("; [DllDir]:" + _dllDir);
            builder.Append("; [ScanInterval]:" + _scanInterval);
            return builder.ToString();
        }
    }
}
